//
//  RoseMarketViewController.m
//  ROSE
//
//  Created by 艾阳 on 2018/5/22.
//  Copyright © 2018年 HeNanZhongDunYunAn. All rights reserved.
//

#import "RoseMarketViewController.h"
#import "QZConditionFilterView.h"
#import "UIView+Extension.h"
#import "LMJRequestManager.h"
#import "RoseCell.h"
#import "RoseModel.h"
#import "RoseDetailViewController.h"

@interface RoseMarketViewController ()<LMJElementsFlowLayoutDelegate,LMJNavUIBaseViewControllerDataSource>
{
    NSArray * _selectedData1Ary;
    NSArray * _selectedData2Ary;
    NSArray * _selectedData3Ary;
}

@property (nonatomic,strong) QZConditionFilterView *conditionFilterView;

@property (nonatomic,strong) NSMutableArray *roseListArray;



@end

@implementation RoseMarketViewController

- (NSMutableArray *)roseListArray{
    if (!_roseListArray) {
        _roseListArray = [[NSMutableArray alloc]init];
    }
    return _roseListArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.title = @"玫瑰花园";
    [self configureFilterView];
    [self getMarketListWithStart:0 count:10];
    [self.collectionView registerClass:[RoseCell class] forCellWithReuseIdentifier:NSStringFromClass([RoseCell class])];
    [self changeCollectionViewFrame];
    
}
/**
 调整列表位置
 */
- (void)changeCollectionViewFrame
{
    UIEdgeInsets contentInset = self.collectionView.contentInset;
    /**
     为顶部选择器 空出位置，所以加了30，重复NavBar的原因再减去64
     */
    contentInset.top += 40;
    
    self.collectionView.contentInset = contentInset;
}
#pragma mark - DataRequest
/**
 获取市场列表
 */
- (void)getMarketListWithStart:(NSInteger)start count:(NSInteger)count{
    NSDictionary * parameters  = @{@"SORT":@"SJSJ",@"SORTRULE":@"ASC",@"start":[NSString stringWithFormat:@"%ld",(long)start],@"count":[NSString stringWithFormat:@"%ld",count]};
    [[LMJRequestManager sharedManager] POST:[NSString stringWithFormat:@"%@/market/market",url_base] parameters:parameters completion:^(LMJBaseResponse *response) {
        if (response.error == NULL) {
            if ([response.responseObject[@"errorcode"] isEqualToString:@"000000"]) {
                NSArray *listArray = response.responseObject[@"dataobj"];
                [self dataConvert:listArray];
            }
            [self.collectionView reloadData];
        }
    }];
}

#pragma mark -DaraConvert
- (void)dataConvert:(NSArray *)listArray{
    for (NSDictionary * roseDetailDict in listArray) {
        RoseModel *roseModel = [[RoseModel alloc]init];
        [roseModel setValuesForKeysWithDictionary:roseDetailDict];
        [self.roseListArray addObject:roseModel];
    }
}

#pragma mark - CollectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.roseListArray.count==0?0:self.roseListArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RoseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([RoseCell class]) forIndexPath:indexPath];
    [cell setDataWithRoseModel: self.roseListArray[indexPath.row]];
    return cell;
}


/**
 跳转进详情页面
 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    RoseModel *roseModel = self.roseListArray[indexPath.row];
    RoseDetailViewController * roseDetailVc = [[RoseDetailViewController alloc]initWithRoseID:roseModel.ID];
    [self.navigationController pushViewController:roseDetailVc animated:YES];
}


#pragma mark - CollectionViewFlowLayout

/**
 Cell-Size
 */
- (CGSize)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout collectionView:(UICollectionView *)collectionView sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREEN_WIDTH-85)/2, (SCREEN_WIDTH-85)/2*(625.0/518.0));
}
//384
- (UIEdgeInsets)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout edgeInsetsInCollectionView:(UICollectionView *)collectionView
{
    return UIEdgeInsetsMake(20, 35, 10, 35);
}
- (CGFloat)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout collectionView:(UICollectionView *)collectionView columnsMarginForItemAtIndexPath:(NSIndexPath *)indexPath{
    return 15;
}


#pragma mark - CollectionViewControllerDataSource

- (UICollectionViewLayout *)collectionViewController:(LMJCollectionViewController *)collectionViewController layoutForCollectionView:(UICollectionView *)collectionView{
    
    LMJElementsFlowLayout * elementsFlowLayout = [[LMJElementsFlowLayout alloc] initWithDelegate:self];
    return elementsFlowLayout;
}


#pragma mark - Top Filter
/*
 配置顶部选择器
 */
- (void)configureFilterView{
    self.conditionFilterView = [QZConditionFilterView conditionFilterViewWithFilterBlock:^(BOOL isFilter, NSArray *dataSource1Ary, NSArray *dataSource2Ary, NSArray *dataSource3Ary) {
        if (isFilter) {
            //
        }else{
            
        }
        [self startRequest];
    }];
    self.conditionFilterView.y += 64;
    //设置初次加载的默认数据
    _selectedData1Ary = @[@"时间"];
    _selectedData2Ary = @[@"能量"];
    _selectedData3Ary = @[@"价格"];
    //下拉选项
    
    //
    [self.conditionFilterView bindChoseArrayDataSource1:_selectedData1Ary DataSource2:_selectedData2Ary DataSource3:_selectedData3Ary];
    [self.view addSubview:self.conditionFilterView];
}

/*
 根据条件请求
 */
- (void)startRequest{
    
}

/**
 去掉baseNAV重复的NavBar
 */
-(BOOL)navUIBaseViewControllerIsNeedNavBar:(LMJNavUIBaseViewController *)navUIBaseViewController{
    return NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
